//
//  ProgressView.swift
//  weibo1
//
//  Created by changhongliang on 2017/8/11.
//  Copyright © 2017年 richinfo. All rights reserved.
//

import UIKit

class ProgressView: UIView {

    /// 进度 0~1
    var progress: CGFloat = 0.0
    {
        didSet{
            // 只要收到新的数据就绘制圆形
            setNeedsDisplay()
        
        }
    
    
    }
    


    override func draw(_ rect: CGRect) {
        // 控制下载进度,如果完成就消失
        if progress >= 1.0 {
            return
        }
        //画图
        //圆心
        let center = CGPoint(x: bounds.size.width * 0.5, y: bounds.size.height * 0.5)
        //半径
        let radius = min(bounds.size.width * 0.5, bounds.size.height * 0.5)
        //开始位置
        let start = -CGFloat.pi/2
        //结束位置
        let end = CGFloat.pi * 2 * progress + start
        //绘制圆形
        let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: start, endAngle: end, clockwise: true)
        //连接圆心
        path.addLine(to: center)
        //关闭路径
        path.close()
        UIColor(white: 0.5, alpha: 0.8).setFill()
        path.fill()
    }
 

}


/*
 Marks the receiver’s entire bounds rectangle as needing to be redrawn.
You can use this method or the setNeedsDisplay(_:) to notify the system that your view’s contents need to be redrawn. This method makes a note of the request and returns immediately. The view is not actually redrawn until the next drawing cycle, at which point all invalidated views are updated.
Note
If your view is backed by a CAEAGLLayer object, this method has no effect. It is intended for use only with views that use native drawing technologies (such as UIKit and Core Graphics) to render their content.
You should use this method to request that a view be redrawn only when the content or appearance of the view change. If you simply change the geometry of the view, the view is typically not redrawn. Instead, its existing content is adjusted based on the value in the view’s contentMode property. Redisplaying the existing content improves performance by avoiding the need to redraw content that has not changed.
 */
